package cn.ffcs.uec.outStaffCorrectApply.component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zk.ui.SuspendNotAllowedException;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zul.Div;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.raptornuke.portal.kernel.annotation.Transactional;
import cn.ffcs.uec.common.manager.CustomFilesManager;
import cn.ffcs.uec.common.util.NodeVoUtil;
import cn.ffcs.uec.common.util.UecConstants;
import cn.ffcs.uec.common.vo.UecBdPsncl;
import cn.ffcs.uec.organization.manager.UecOrgDeptManager;
import cn.ffcs.uec.organization.manager.UecOrgOrgsManager;
import cn.ffcs.uec.organization.model.UecOrgDept;
import cn.ffcs.uec.organization.model.UecOrgOrgs;
import cn.ffcs.uec.organization.vo.OrganizationRelationVo;
import cn.ffcs.uec.outStaff.constant.UecOutStaffConstants;
import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.outStaff.vo.UecOutStaffInfoVo;
import cn.ffcs.uec.outStaffCorrectApply.component.bean.OutStaffCorrectApplyListboxExtBean;
import cn.ffcs.uec.post.manager.UecPostManager;
import cn.ffcs.uec.post.model.UecPost;
import cn.ffcs.uec.recordsApproval.constant.ApprovalConstants;
import cn.ffcs.uec.recordsApproval.util.ApprovalUtil;
import cn.ffcs.uec.staff.manager.UecPsnjobManager;
import cn.ffcs.uec.staff.manager.UecTrialManager;
import cn.ffcs.uec.staff.model.UecPsnjob;
import cn.ffcs.uec.staff.model.UecTrial;
import cn.ffcs.uom.common.constants.BaseUnitConstants;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.DateUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PlatformUtil;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.NodeVo;
import lombok.Getter;
import lombok.Setter;

@Controller
@Scope("prototype")
public class OutStaffCorrectApplyListboxExt extends Div implements IdSpace {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1142595724524044326L;

	private static final String OUT_STAFF_CORRECT_APPLY_LISTBOX_EXT_ZUL = "/pages_nmg/outStaffCorrectApply/comp/outStaffCorrectApply_listbox_ext.zul";

	/**
	 * 页面对应的bean
	 */
	@Getter
	@Setter
	private OutStaffCorrectApplyListboxExtBean bean = new OutStaffCorrectApplyListboxExtBean();

	/**
	 * 外包人员基本信息对象
	 */
	private UecOutStaffInfo uecOutStaffInfo;
	private UecOutStaffInfo qryUecOutStaffInfo;

	/**
	 * 外包人员虚拟对象
	 */
	private UecOutStaffInfoVo uecOutStaffInfoVo;

	/**
	 * 人员试用情况
	 */
	private UecTrial uecTrial;
	private UecTrial qryUecTrial;

	/**
	 * 任职信息
	 */
	private UecPsnjob qryUecPsnjob;
	private UecPsnjob uecPsnjob;
	/**
	 * 临时任职信息对象
	 */
	private UecPsnjob temporaryUecPsnjob = new UecPsnjob();
	/**
	 * 人员类别
	 */
	private UecBdPsncl uecBdPsncl;
	/**
	 * 人员类别
	 */
	private UecBdPsncl uecBdPsnclTree;
	/**
	 * 部门
	 */
	private UecOrgDept findUecOrgDept;

	/**
	 * 岗位
	 */
	private UecPost uecPost;
	/**
	 * 选择的组织
	 */
	private OrganizationRelationVo organization;

	/**
	 * 外包人员基本信息更新标志
	 */
	private boolean outStaffInfoUpdateFlag = false;
	/**
	 * 任职信息更新、新增标志
	 */
	private boolean uecPsnjobFalg = false;
	/**
	 * 岗位序列
	 */
	private String postSequence = null;

	private UecOrgOrgsManager uecOrgOrgsManager = (UecOrgOrgsManager) ApplicationContextUtil
			.getBean("uecOrgOrgsManager");

	private UecTrialManager uecTrialManager = (UecTrialManager) ApplicationContextUtil
			.getBean("uecTrialManager");

	private UecPsnjobManager uecPsnjobManager = (UecPsnjobManager) ApplicationContextUtil
			.getBean("uecPsnjobManager");

	private UecOrgDeptManager uecOrgDeptManager = (UecOrgDeptManager) ApplicationContextUtil
			.getBean("uecOrgDeptManager");

	private UecPostManager uecPostManager = (UecPostManager) ApplicationContextUtil
			.getBean("uecPostManager");

	private CustomFilesManager customFilesManager = (CustomFilesManager) ApplicationContextUtil
			.getBean("customFilesManager");

	@Getter
	@Setter
	private IPortletInfoProvider portletInfoProvider;

	public OutStaffCorrectApplyListboxExt() {
		Executions.createComponents(OUT_STAFF_CORRECT_APPLY_LISTBOX_EXT_ZUL,
				this, null);
		Components.wireVariables(this, bean);
		Components.addForwards(this, this, '$');

		this.bean.getUecPostBandboxExt().addForward("onUecPostEditSelected",
				this, "onUecPostEditSelectedResponse");
		this.bean.getOutStaffPkDept().addForward("onGetOrganization", this,
				"onGetOrganizationResponse");
		this.bean.getOutPsncl().addForward("onGetBdPsncl", this, "onGetBdPsnclResponse");
		
	}

	/**
	 * 界面初始化
	 */
	public void onCreate() {
		bandListbox();
		bean.getUecPostBandboxExt().setDisabled(true);
	}
	
	/**
	 * 监听人员类别组织树
	 */
	public void onGetBdPsnclResponse(final ForwardEvent event) {
		if (event.getOrigin().getData() != null) {
			uecBdPsnclTree = (UecBdPsncl) event.getOrigin().getData();
		}
	}

	/**
	 * 监听UecOrgPostBandboxExt传出的事件
	 * 
	 * @param event
	 */
	public void onUecPostEditSelectedResponse(final ForwardEvent event) {
		if (event.getOrigin().getData() != null) {
			uecPost = (UecPost) event.getOrigin().getData();
			if (null != uecPost) {
				bean.getUecPostBandboxExt().setValue(uecPost.getPostname());
				//岗位序列
				postSequence = uecPost.getPkPostseries();
				// 岗位序列 --页面赋值
				if ("1001A1100000000006R2".equals(postSequence)) {
					bean.getPkPostseries().setValue("管理序列");
				} else if ("1001A1100000000006R3".equals(postSequence)) {
					bean.getPkPostseries().setValue("专业序列");
				}
			}
		}
	}

	/**
	 * 监听组织树插件抛出的事件
	 * 
	 * @param event
	 */
	public void onGetOrganizationResponse(final ForwardEvent event) {
		if (event.getOrigin().getData() != null) {
			organization = (OrganizationRelationVo) event.getOrigin().getData();
			if ("ORG".equals(organization.getOrgType())) {
				ZkUtil.showError("转正后部门选项请选择到部门层级!", "错误提示");
				// 清空部门
				bean.getOutStaffPkDept().setValue(null);
				// 清空组织
				bean.getHrorg().setValue(null);
				// 清空岗位
				bean.getUecPostBandboxExt().setValue(null);
				// 清空岗位序列
				bean.getPkPostseries().setValue(null);
				bean.getUecPostBandboxExt().setDisabled(true);
				return;
			} else {
				// 根据部门主键：pkorg，找到该部门所在的公司
				findUecOrgDept = new UecOrgDept();
				findUecOrgDept.setPkDept(organization.getOrg());
				findUecOrgDept = uecOrgDeptManager
						.findUecOrgDeptByOrgDept(findUecOrgDept);
				String uecOrgOrgsNameByPkOrg = uecOrgOrgsManager
						.getUecOrgOrgsNameByPkOrg(findUecOrgDept
								.getPkOrg());
				if (null != findUecOrgDept) {
					String code = PlatformUtil.getCurrentUser().getScreenName().toUpperCase();
					
					String[] orgnamelist= {"呼伦","兴安","通辽","赤峰","锡林","乌兰","乌海"};
					boolean   temp=false;
					if("W2019100".equals(code) || "W2019101".equals(code) || "W2019102".equals(code)) {
						      for(int i=0;i<orgnamelist.length;i++) {
								if( !uecOrgOrgsNameByPkOrg.contains(orgnamelist[i])) {
								  temp=true;
								  break;
								}
						}
					if(temp) {
							// 清空部门
							bean.getOutStaffPkDept().setValue(null);
							// 清空组织
							bean.getHrorg().setValue(null);
							// 清空岗位
							bean.getUecPostBandboxExt().setValue(null);
							// 清空岗位序列
							bean.getPkPostseries().setValue(null);
							bean.getUecPostBandboxExt().setDisabled(true);
							return;
						}	      
					}
					bean.getUecPostBandboxExt().setDisabled(false);
					uecPsnjobFalg = true;
					bean.getHrorg().setValue(uecOrgOrgsNameByPkOrg);
					bean.getUecPostBandboxExt().setValue(null);
					Events.postEvent("onUecDeptSelect", this.bean
							.getUecPostBandboxExt().getBean()
							.getUecPostListboxExt(), findUecOrgDept);
				} else {
					uecPsnjobFalg = false;
				}
			}
		}
	}

	/**
	 * 查询按钮 根据条件查询
	 * 
	 * @throws Exception
	 * @throws SuspendNotAllowedException
	 */
	public void onQuery() throws SuspendNotAllowedException, Exception {
		clearVariable();
		uecOutStaffInfoVo = new UecOutStaffInfoVo();
		/**
		 * 组织树Ext UecOrganizationTreeBandboxExt 组织树上有组织和部门 通过组织树获取组织或部门
		 */
		if (!StrUtil.isNullOrEmpty(bean.getOutStaffPkHrorg().getValue())) {
			// 组织类型：公司 or 部门
			String orgType = (String) bean.getOutStaffPkHrorg()
					.getOrganization().getOrgType();
			// 组织主键
			String pkorg = (String) bean.getOutStaffPkHrorg().getOrganization()
					.getOrg();
			if ("ORG".equals(orgType)) {
				String orgName = (String) bean.getOutStaffPkHrorg().getOrganization()
						.getOrgName();
				// 公司
				//uecOutStaffInfoVo.setPkOrg(pkorg);
				 if("中国电信股份有限公司内蒙古分公司".equals(orgName)) {
			       		uecOutStaffInfoVo
			       		.setOrgName(null);
			       	}else if(orgName.endsWith("*")){
			       		uecOutStaffInfoVo
			       		.setOrgName(orgName);
			       	}else {
			       		uecOutStaffInfoVo.setPkOrg(pkorg);
			       	}
			} else {
				// 部门
				UecOrgDept uecOrgDept = new UecOrgDept();
				UecOrgOrgs uecOrgOrgs = new UecOrgOrgs();
				uecOrgDept.setPkDept(pkorg);
				uecOrgDept = uecOrgDeptManager.findUecOrgDeptByOrgDept(uecOrgDept);
				// 部门所属公司
				if(uecOrgDept !=null) {
					uecOrgOrgs= new UecOrgOrgs();
					uecOrgOrgs.setPkOrg(uecOrgDept.getPkOrg());
					uecOrgOrgs = uecOrgOrgsManager.findUecOrgOrgsByUecOrgOrgs(uecOrgOrgs);
					String orgName=uecOrgOrgs.getName();
					uecOutStaffInfoVo.setPkDept(uecOrgDept.getPkDept());
					uecOutStaffInfoVo.setPkOrg(uecOrgDept.getPkOrg());
					uecOutStaffInfoVo.setOrgName(orgName);

				//uecOutStaffInfoVo.setPkDept(pkorg);
			}
			}
		}
		uecOutStaffInfoVo.setStaffCode(bean.getOutStaffCode().getValue());
		uecOutStaffInfoVo.setStaffName(bean.getOutStaffName().getValue());
		uecOutStaffInfoVo.setIdNum(bean.getOutStaffIdNum().getValue());
		// 查询员工
		outStaffList();
	}

	/**
	 * 员工列表
	 * 
	 * @throws Exception
	 * @throws SuspendNotAllowedException
	 */

	public void outStaffList() throws SuspendNotAllowedException, Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("opType", "outStaffList");
		map.put("uecOutStaffInfoVo", uecOutStaffInfoVo);
		Window window = (Window) Executions.createComponents(
				UecOutStaffConstants.UEC_OUT_STAFF_LIST_ZUL, this, map);
		window.doModal();
		window.addEventListener("onOK", new EventListener() {
			@Override
			public void onEvent(Event event) throws Exception {
				if (event.getData() != null) {
					qryUecOutStaffInfo = (UecOutStaffInfo) event.getData();
					uecOutStaffInfo = new UecOutStaffInfo();
					uecOutStaffInfo = qryUecOutStaffInfo;
					/**
					 * 每个员工只有一次转正申请
					 */
					qryUecTrial = new UecTrial();
					qryUecTrial.setStaffId(qryUecOutStaffInfo.getOutStaffId());
					qryUecTrial
							.setNmStaffType(BaseUnitConstants.NM_OUT_STAFF_TYPE);
					qryUecTrial = uecTrialManager.findUecTrial(qryUecTrial);
					// 判断 人员试用情况是否为null
					if (!StrUtil.isNullOrEmpty(qryUecTrial)) {
						uecTrial = UecTrial.newInstance();
						uecTrial = qryUecTrial;
						Long trialresult = qryUecTrial.getTrialresult();
						if (null != trialresult) {
							if (1L == trialresult) {
								ZkUtil.showError("此人已转正", "温馨提示");
								return;
							}
						}
						// 任职信息中有岗位
						qryUecPsnjob = new UecPsnjob();
						qryUecPsnjob.setStaffId(qryUecOutStaffInfo
								.getOutStaffId());
						qryUecPsnjob
								.setNmStaffType(BaseUnitConstants.NM_OUT_STAFF_TYPE);
						qryUecPsnjob = uecPsnjobManager
								.findUecPsnjob(qryUecPsnjob);
						
						temporaryUecPsnjob=qryUecPsnjob;
						if (null != qryUecPsnjob) {
							// 组织
							bean.getOrgName()
									.setValue(
											uecOrgOrgsManager
													.getUecOrgOrgsNameByPkOrg(qryUecPsnjob
															.getPkOrg()));
							// 部门
							bean.getDeptName()
									.setValue(
											uecOrgDeptManager
													.getUecOrgDeptNameByPkDept(qryUecPsnjob
															.getPkDept()));
							// 岗位
							if (!StrUtil.isNullOrEmpty(qryUecPsnjob.getPkPost())) {
								UecPost uecPost = UecPost.newInstance();
								uecPost.setPkPost(qryUecPsnjob.getPkPost());
								uecPost = uecPostManager.queryUecPost(uecPost);
								if (null != uecPost) {
									bean.getPostName().setValue(
											uecPost.getPostname());
								}
							} else {
								bean.getPostName().setValue(
										qryUecPsnjob.getOutJobName());
							}
							
							// 人员类别
							uecBdPsncl = new UecBdPsncl();
							uecBdPsncl.setPkPsncl(qryUecPsnjob.getPkPsncl());
							uecBdPsncl = customFilesManager
									.findUecUecBdPsnclByUecBdPsncl(uecBdPsncl);
							if (!StrUtil.isNullOrEmpty(uecBdPsncl)) {
								bean.getPersonnelCategory().setValue(
										uecBdPsncl.getName());
							}
							// 岗位序列
							if ("1001A1100000000006R3".equals(qryUecPsnjob
									.getPkPostseries())) {
								bean.getPostSequence().setValue("专业序列");
							} else if ("1001A1100000000006R2"
									.equals(qryUecPsnjob.getPkPostseries())) {
								bean.getPostSequence().setValue("管理序列");
							} else {
								bean.getPostSequence().setValue(null);
							}
							// 岗位层级
							/*bean.getPostLevel()
									.setValue(
											customFilesManager
													.findCustomFilesByPkdefdoc(qryUecPsnjob
															.getJobglbdef2()));*/
							// 岗位层级
							bean.getBeforenewPostLevel()
									.setValue(
											customFilesManager
													.findCustomFilesByPkdefdoc(qryUecPsnjob
															.getJobglbdef2()));
							// 岗位等级
							bean.getBeforepostGrade()
									.setValue(
											customFilesManager
													.findCustomFilesByPkdefdoc(qryUecPsnjob
															.getJobglbdef3()));
							// 职务名称
							bean.getPositionName().setValue(
									qryUecPsnjob.getJobglbdef4());
							// 任职原因
							bean.getAppointmentReason().setValue(
									qryUecPsnjob.getJobglbdef5());
							// 任职文号
							bean.getAppointmentNumber().setValue(
									qryUecPsnjob.getJobglbdef6());
							// 任本岗位层级时间
							bean.getRenaultLevelTime().setValue(
									qryUecPsnjob.getJobglbdef8());
							// 任现岗位层级时间
							bean.getRenaultGradeTime().setValue(
									qryUecPsnjob.getJobglbdef14());
						} else {
							bean.getDeptName().setValue(null);
							bean.getPostName().setValue(null);
						}
						PubUtil.fillBeanFromPo(qryUecTrial, bean);
						// 试用类型
						if (qryUecTrial.getTrialType() == 1L) {
							bean.getTrialTypeName().setValue("转岗试用");
						} else if (qryUecTrial.getTrialType() == 2L) {
							bean.getTrialTypeName().setValue("入职试用");
						}
						// 转正人员编码
						bean.getPsnCode().setValue(
								qryUecOutStaffInfo.getPsnCode());
						// 转正人员姓名
						bean.getPsnName().setValue(
								qryUecOutStaffInfo.getPsnName());
						// 试用开始日期
						bean.getBegindate().setValue(qryUecTrial
										.getBegindate());
						// 试用结束日期
						bean.getEnddate()
								.setValue(qryUecTrial
												.getEnddate());
					} else {
						ZkUtil.showError("此人未入职,请先执行入职操作", "温馨提示");
					}
				}
			}
		});
	}

	/**
	 * 转正申请流程发起按钮
	 * 
	 * @throws Exception
	 */
	@Transactional(rollbackFor = Exception.class)
	public void onLaunchCorrectApply() throws Exception {
		if (null == uecOutStaffInfo || null == uecOutStaffInfo.getOutStaffId()) {
			ZkUtil.showError("未获取人员！", "错误提示");
			return;
		}
		// 数据校验
		String msg = this.checkUacStaffData();
		if (null != msg) {
			Messagebox.show(msg);
			return;
		}
		// 获取登录用户
		String currentName = "";
		if (StrUtil.isNullOrEmpty(PlatformUtil.getCurrentUser())) {
			ZkUtil.showError("当前登录用户已经失效，请重新登录系统", "错误提示");
			return;
		} else {
			currentName = PlatformUtil.getCurrentUser().getScreenName();
		}

		/**
		 * 生效时间： 生效时间为调配后新的的任职信息的开始时间，为调配前旧的任职信息的结束时间
		 */
		String effetDate = DateUtil.dateToStr(bean.getEffectDate().getValue());

		/**
		 * 1：盟市内转正：组织不变，部门可变、岗位可变 2：盟市间转正：组织可变、部门可变、岗位可变
		 */
		// 转正时修改任职信息
		if (null != qryUecPsnjob) {
			if (null != findUecOrgDept) {
				/**
				 * 1:区本部的人员可以对某个盟市的人员进行跨组织转正，即转正后所在的组织跟部门非当前组织部门
				 * 2:某个盟市的主管对该盟市人员进行转正，只能是组织不动、调部门和岗位
				 */
				// 更新人员基本信息中的组织
				if (StrUtil.isNullOrEmpty(uecOutStaffInfo.getPkHrorg()) || !uecOutStaffInfo.getPkHrorg().equals(
						findUecOrgDept.getPkOrg())) {
					uecOutStaffInfo.setPkHrorg(findUecOrgDept.getPkOrg());
					uecOutStaffInfo.setModifier(currentName);
					uecOutStaffInfo.setModifiedTime(DateUtil.getCurrenDate());
					outStaffInfoUpdateFlag = true;
				} else {
					outStaffInfoUpdateFlag = false;
				}
				// 如果更改任职信息表中的组织、部门、岗位，即新增一条任职信息
				uecPsnjob = new UecPsnjob();
				// 同步员工的id
				uecPsnjob.setStaffId(uecOutStaffInfo.getOutStaffId());
				// 外包标识
				uecPsnjob.setNmStaffType(BaseUnitConstants.NM_OUT_STAFF_TYPE);
				// 同步集团默认主键
				uecPsnjob.setPkPsndoc(uecOutStaffInfo.getPkPsnMsg());
				// 获取人员类别
				/*uecPsnjob.setPkPsncl((String) bean.getPkPsncl()
						.getSelectedItem().getValue());*/
				if(null != uecBdPsnclTree) {
					String pkpsncl = uecBdPsnclTree.getPkPsncl();
					uecPsnjob.setPkPsncl(pkpsncl);
				}
				// 获取岗位序列
				if(null != postSequence) {
					uecPsnjob.setPkPostseries(postSequence);
				}
				// 获取岗位层级
				/*uecPsnjob.setJobglbdef2((String) bean.getJobglbdef2()
						.getSelectedItem().getValue());*/
				// 获取岗位等级
				/*uecPsnjob.setJobglbdef3((String) bean.getJobglbdef3()
						.getSelectedItem().getValue());*/
				
				//岗位等级（新）
				if(bean.getNewPostLevel().getUecBdDefdocs()!=null) {
					String uecPsncl=bean.getNewPostLevel().getUecBdDefdocs().get(0).getName();
					String uecPsnclOld="";
					if(!StrUtil.isNullOrEmpty(temporaryUecPsnjob.getJobglbdef3Name())) {
						uecPsnclOld=temporaryUecPsnjob.getJobglbdef3Name();
					}
					if(!uecPsncl.equals(uecPsnclOld)) {
						if(!StrUtil.isNullOrEmpty(uecPsncl)) {
							uecPsnjob.setJobglbdef3(bean.getNewPostLevel().getUecBdDefdocs().get(0).getPkDefdoc());
						}
					}else {
						uecPsnjob.setJobglbdef3(temporaryUecPsnjob.getJobglbdef3());
					}
				}else {
					uecPsnjob.setJobglbdef3(temporaryUecPsnjob.getJobglbdef3());
				}
				//岗位层级（新）
				if(bean.getPostGrade().getUecBdDefdocs()!=null) {
					String uecPsncl=bean.getPostGrade().getUecBdDefdocs().get(0).getName();
					String uecPsnclOld="";
					if(!StrUtil.isNullOrEmpty(temporaryUecPsnjob.getJobglbdef2Name())) {
						uecPsnclOld=temporaryUecPsnjob.getJobglbdef2Name();
					}
					if(!uecPsncl.equals(uecPsnclOld)) {
						if(!StrUtil.isNullOrEmpty(uecPsncl)) {
							uecPsnjob.setJobglbdef2(bean.getPostGrade().getUecBdDefdocs().get(0).getPkDefdoc());
						}
					}else {
						uecPsnjob.setJobglbdef2(temporaryUecPsnjob.getJobglbdef2());
					}
				}else {
					uecPsnjob.setJobglbdef2(temporaryUecPsnjob.getJobglbdef2());
				}
				
				
				
				
				// 获取职务名称
				uecPsnjob.setJobglbdef4(bean.getJobglbdef4().getValue());
				// 获取任职原因
				uecPsnjob.setJobglbdef5(bean.getJobglbdef5().getValue());
				// 获取任职文号
				uecPsnjob.setJobglbdef6(bean.getJobglbdef6().getValue());
				// 获取组织
				uecPsnjob.setPkOrg(findUecOrgDept.getPkOrg());
				uecPsnjob.setPkHrorg(findUecOrgDept.getPkOrg());
				// 获取部门
				uecPsnjob.setPkDept(findUecOrgDept.getPkDept());
				// 获取岗位
				if (null != uecPost) {
					uecPsnjob.setPkPost(uecPost.getPkPost());
					//获取三级基准岗位bmp
					uecPsnjob.setJobglbdef24(uecPost.getPkPoststd());
				}
				// 获取任本岗位层级时间
				uecPsnjob.setJobglbdef8(DateUtil.dateToStr(bean.getJobglbdef8()
						.getValue()));
				// 任现岗位层级时间
				uecPsnjob.setJobglbdef14(DateUtil.dateToStr(bean
						.getJobglbdef14().getValue()));
				// 人员状态设置为：新进
				uecPsnjob.setPsntype(0L);
				// 是否主职：是
				uecPsnjob.setIsmainjob("Y");
				// 是否结束：是
				uecPsnjob.setEndflag("N");
				// 是否最新记录
				uecPsnjob.setLastflag("Y");
				// 是否在岗：是
				uecPsnjob.setPoststat("Y");
				// 是否试用：否
				uecPsnjob.setTrialFlag("N");
				// 调配后任职信息的开始时间
				uecPsnjob.setBegindate(effetDate);
				// 修改 相应的创建人、创建时间
				uecPsnjob.setCreator(currentName);
				uecPsnjob.setCreationtime(DateUtil.getCurrenDate());
			}
			/**
			 * 生效时间不为空，将调配前的任职信息的结束日期更新为生效时间
			 */
			qryUecPsnjob.setEnddate(effetDate);
			// 人员状态设置为退出
			qryUecPsnjob.setPsntype(2L);
			// 是否主职：否
			qryUecPsnjob.setIsmainjob("N");
			// 是否结束：是
			qryUecPsnjob.setEndflag("Y");
			// 是否最新记录：否
			qryUecPsnjob.setLastflag("N");
			// 是否在岗：否
			qryUecPsnjob.setPoststat("N");
			// 修改人
			qryUecPsnjob.setModifier(currentName);
			// 修改时间
			qryUecPsnjob.setModifiedtime(DateUtil.getCurrenDate());
		}

		// 获取人员试用情况数据
		Long trialResult = Long.parseLong((String) bean.getTrialresult()
				.getSelectedItem().getValue());
		// 1L 代表转正，若转正结束标志设置为Y
		if (1L == trialResult) {
			uecTrial.setEndflag("Y");
		} else {
			uecTrial.setEndflag("N");
		}
		uecTrial.setTrialresult(trialResult);

		// 获取转正日期
		uecTrial.setRegulardate(DateUtil.dateToStr(bean.getRegulardate()
				.getValue()));
		uecTrial.setModifier(currentName);
		uecTrial.setModifiedtime(DateUtil.getCurrenDate());

		if (ApprovalUtil.getActivityFlag()) {
			// 判断该人员是否在审状态，如果是在审状态不允许再次发起此流程
			boolean isOnApproval = ApprovalUtil.isOnApproval(
					ApprovalConstants.OUT_STAFF_CORRECT_PROC_DEF_ID,
					uecOutStaffInfo.getOutStaffId());
			if (isOnApproval) {
				ZkUtil.showError("在审状态不允许发起！", "错误提示");
				return;
			}
			if (StrUtil.isNullOrEmpty(PlatformUtil.getCurrentUser())) {
				ZkUtil.showError("当前登录用户不存在!", "错误提示");
				return;
			}
			// uec_out_staff_info 修改一条记录，uec_trial 修改一条记录,
			// uec_psnjob修改一条记录,uec_psnjob新增一条记录
			// 这里不作判断，uec_out_staff_info、uec_psnjob都添加一条审批记录
			// 数据入库
			Long uecOutStaffIdHis = uecOutStaffInfo.getOutStaffId();
			uecOutStaffInfo.addStatusCdIS3();
			Long uecOutStaffId = uecOutStaffInfo.getOutStaffId();

			Long uecPsnjobIdHis = qryUecPsnjob.getPsnjobId();
			qryUecPsnjob.setStaffId(uecOutStaffIdHis);
			qryUecPsnjob.setNmStaffType(BaseUnitConstants.NM_OUT_STAFF_TYPE);
			qryUecPsnjob.addStatusCdIS3();
			Long uecPsnjobIdModify = qryUecPsnjob.getPsnjobId();
			uecPsnjob.addStatusCdIS3();
			Long uecPsnjobIdAdd = uecPsnjob.getPsnjobId();

			Long uecTrialIdHis = uecTrial.getTrialId();
			uecTrial.setStaffId(uecOutStaffIdHis);
			uecTrial.setNmStaffType(BaseUnitConstants.NM_OUT_STAFF_TYPE);
			uecTrial.addStatusCdIS3();
			Long uecTrialId = uecTrial.getTrialId();

			String formTheme = "盟市外包人员转正流程";

			String startFlowJson = "{" + "formTheme: '" + formTheme + "',"
					+ " formContent: '转正人员:" + uecOutStaffInfo.getPsnName()
					+ "'," + " procDefId: '"
					+ ApprovalConstants.OUT_STAFF_CORRECT_PROC_DEF_ID + "',"
					+ " urgent: '1'," + " atiActCategoryId: '"
					+ ApprovalConstants.CORRECT_CATEGORY_ID + "',"
					+ " procDefKey: '" + ApprovalUtil.OUT_STAFF_TRIAL + "',"
					+ " formSender: '" + currentName + "'," + " primaryId: '{"
					+ "uec_out_staff_info_his:" + uecOutStaffIdHis
					+ ",uec_out_staff_info:" + uecOutStaffId
					+ ",uec_psnjob_his:" + uecPsnjobIdHis + ",uec_psnjob_modi:"
					+ uecPsnjobIdModify + ",uec_psnjob_add:" + uecPsnjobIdAdd
					+ ",uec_trial_his:" + uecTrialIdHis + ",uec_trial:"
					+ uecTrialId + "'}";

			String procInstId = null;
			try {
				procInstId = ApprovalUtil.startApproval(startFlowJson);
			} catch (Exception e) {
				// 流程发起失败 数据改为失效
				uecOutStaffInfo.removeOnly();
				uecTrial.removeOnly();
				qryUecPsnjob.removeOnly();
				onReset();
				ZkUtil.showError("流程发起失败！", "错误提示");
				e.printStackTrace();
			}
			if (StrUtil.isNullOrEmpty(procInstId)) {
				uecOutStaffInfo.removeOnly();
				uecTrial.removeOnly();
				qryUecPsnjob.removeOnly();
				onReset();
				ZkUtil.showError("流程发起失败！", "错误提示");
				return;
			}

			// 第一审批人如果是发起人直接到人力审批环节
			ApprovalUtil.isCurrentAssignNames(procInstId);

			ApprovalUtil.showNextAssignee(procInstId, formTheme);
			onReset();
		} else {
			// 直接更新数据
			if (outStaffInfoUpdateFlag) {
				uecOutStaffInfo.updateOnly();
			}
			uecTrial.setStaffId(uecOutStaffInfo.getOutStaffId());
			uecTrial.updateOnly();
			if (uecPsnjobFalg) {
				// 转正前任职信息更新
				qryUecPsnjob.updateOnly();
				// 转正后任职信息新增
				uecPsnjob.addOnly();
			}
			ZkUtil.showInformation("数据保存成功", "温馨提示");
			onReset();
		}

	}

	/**
	 * 检测填入的参数是否为空
	 */
	public String checkUacStaffData() {
		if (StrUtil.isNullOrEmpty(bean.getTrialresult().getSelectedItem().getValue())) {
			return "请选择试用结果";
		} else if (StrUtil.isNullOrEmpty(bean.getRegulardate().getValue())) {
			return "请选择转正日期";
		} else if (StrUtil.isNullOrEmpty(bean.getEffectDate().getValue())) {
			return "请选择生效日期";
		} else if (StrUtil.isNullOrEmpty(bean.getOutStaffPkDept().getValue())) {
			return "请选择部门";
		} else if (StrUtil.isNullOrEmpty(bean.getUecPostBandboxExt().getValue())) {
			return "请选择岗位";
		} else if (StrUtil.isNullOrEmpty(bean.getOutPsncl().getValue())) {
			return "请选择人员类别";
		} 
		/*else if(StrUtil.isNullOrEmpty(bean.getJobglbdef2().getSelectedItem().getValue())) {
			return "请选择岗位层级";
		}else if(StrUtil.isNullOrEmpty(bean.getJobglbdef3().getSelectedItem().getValue())) {
			return "请选择岗位等级";
		}*/
		else if(StrUtil.isNullOrEmpty(bean.getNewPostLevel().getUecBdDefdocs())) {
			return "请选择岗位层级";
		}else if(StrUtil.isNullOrEmpty(bean.getPostGrade().getUecBdDefdocs())) {
			return "请选择岗位等级";
		}
		return null;
	}

	/**
	 * 重置按钮
	 */
	public void onReset() {
		clearVariable();
		PubUtil.clearComponent(this);
		// 重置自定义组件
		this.bean.getOutStaffPkDept().setValue(null);
		this.bean.getUecPostBandboxExt().getBean().getUecPostListboxExt()
				.onReset();
		Events.postEvent("onCleanUecDeptSelect", this.bean
				.getUecPostBandboxExt().getBean().getUecPostListboxExt(), null);
	}

	/**
	 * 清除变量
	 */
	public void clearVariable() {
		uecTrial = null;
		uecOutStaffInfo = null;
		uecOutStaffInfoVo = null;
	}

	/**
	 * 绑定Listbox
	 */
	public void bandListbox() {
		// 试用类型trialType
		List<NodeVo> nodeVoTrialTypeList = new ArrayList<NodeVo>();
		String[][] trialType = { { "1", "入职试用" }, { "2", "转岗试用" } };
		for (int i = 0; i < trialType.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(trialType[i][j]);
				nodeVo.setName(trialType[i][j + 1]);
			}
			nodeVoTrialTypeList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getTrialType(), nodeVoTrialTypeList);

		// 试用结果trialresult
		List<NodeVo> nodeVoResultList = new ArrayList<NodeVo>();
		String[][] trialresult = { { "1", "转正" }, { "2", "延长试用期" },
				{ "3", "未通过试用" } };
		for (int i = 0; i < trialresult.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(trialresult[i][j]);
				nodeVo.setName(trialresult[i][j + 1]);
			}
			nodeVoResultList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getTrialresult(), nodeVoResultList);

		/**
		 * 人员类别--PSNCL_PK_ORG
		 */
		ListboxUtils.rendererForEdit(bean.getPkPsncl(),
				NodeVoUtil.getDictionariesPsncl(UecConstants.PSNCL_PK_ORG));
		// 岗位层级--JOBGLBDEF2
		ListboxUtils.rendererForEdit(bean.getJobglbdef2(),
				NodeVoUtil.getDictionaries(UecConstants.POST_HIERARCHY));
		// 岗位等级--Jobglbdef3
		ListboxUtils.rendererForEdit(bean.getJobglbdef3(),
				NodeVoUtil.getDictionaries(UecConstants.POST_LEVEL));
	}

}
